package com.longge;

import java.util.Arrays;

/**
 * @创建人 xinglongge
 * @创建时间 2021/12/3
 * @描述
 */
public class LargestSumAfterKNegations {
    public int largestSumAfterKNegations(int[] nums, int k) {

        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= 0 && k != 0) {
                nums[i] = -nums[i];
                k--;
            } else {
                break;
            }
        }
        if (k != 0) {
            Arrays.sort(nums);
            while (k != 0) {
                k--;
                nums[0]=-nums[0];
            }
        }
        int res = 0;
        for (int num : nums) {
            res+=num;
        }
        return res;

    }
}
